#include "Opt_App.h"

module Opt_CO2_AppM {

  uses {
    interface Boot;
    interface Leds;
    interface Timer<TMilli>;

    interface Interaction;
    interface StdControl as SCSuartDBGstd;
    interface SCSuartDBG;
  }

} implementation {

  #define MyOPT_Type OPT_CO2

  norace App_struct_t AP_Frame;
  norace uint8_t MsgBuff[64], myAppType, myOptType, TestSeq=0;

  //void SensorsPrint (uint8_t App_size);

  //////////////////////////////////////////////////////////
  task void TryToConfigure() {call Interaction.StartConfiguration (&AP_Frame);}
  task void TryToSetTimer()  {call Interaction.SetSamplingTime(Default_Sampling_Time);}
  //////////////////////////////////////////////////////////

  event void Boot.booted() {
    call SCSuartDBGstd.start();
    post TryToConfigure();
  }

  //////////////////////////////////////////////////////////

  event void Interaction.StartDoneConfiguration(uint8_t appType, uint8_t optType) {

    sprintf(MsgBuff, "%d myAppType:%X, myOptType:%X\r\n", TOS_NODE_ID, appType, optType);
    call SCSuartDBG.UARTSend(MsgBuff, strlen(MsgBuff));
 
    if (optType != MyOPT_Type) {
	post TryToConfigure();
	return;
    }
    
    call Leds.led0On();

    myAppType = appType;
    myOptType = optType;

    post TryToSetTimer();
  }

  event void Interaction.getSensorDataDone(App_struct_t *App_Payload, uint8_t App_size){
    sprintf(MsgBuff, "%d detects CO2 value:%d\r\n", TOS_NODE_ID, AP_Frame.AppData.sensor.Sdata.ADC_data[0]);
    call SCSuartDBG.UARTSend(MsgBuff, strlen(MsgBuff));
  }

  event void Interaction.Urgency_Data (uint8_t *Urgency_Payload, uint8_t len) {
  }

  //////////////////////////////////////////////////////////

  event void Timer.fired(){
    return;
  }

  //////////////////////////////////////////////////////////

}